<?php
namespace Admin\Controller;

class FuturesOrderController extends AdminController
{
	public function index(){
		$model   =   D('futures_order');
		if($_REQUEST['direction']){//涨跌搜索
            if($_REQUEST['direction']==1){
                $map['direction']=array('eq',1);
                $direction=1;
            }elseif($_REQUEST['direction']==3){
                $map['direction']=array('eq',0);
                $direction=3;
            }elseif($_REQUEST['direction']==2){
                $map['direction']=array('eq',2);
                $direction=2;
            }else{
                $direction=-1;
            }
        }
        else{
            $direction=-1;
        }
        $this->assign('direction',$direction);
        if($_REQUEST['status']){//状态搜索
            if($_REQUEST['status']==1){
                $map['status']=array('eq',1);
                $status=1;
            }elseif($_REQUEST['status']==2){
                $map['status']=array('eq',2);
                $status=2;
            }elseif($_REQUEST['status']==-1){
                $map['status']=array('eq',0);
                $status=-1;
            }else{
				 $status=-2;
			}
        }
        else{
             $status=-2;
        }
        $this->assign('status',$status);
        //开始时间搜索
        $begin_time=I('get.begin_time');
        if($begin_time){
            $map['create_time']=array('egt',strtotime($begin_time));
            $this->assign('begin_time',$begin_time);
        }
        //结束时间搜索
        $end_time=I('get.end_time');
        if($end_time){
            $end_time=strtotime($end_time)+86400;
            $map['_string']='create_time <='.$end_time;
            $this->assign('end_time',$end_time);
        }
         
         //用户昵称搜索
         if($_REQUEST['user_name']){
		 	 $user_name=$_REQUEST['user_name'];
		 	$follow_name['nickname']=array('like',"%".trim($user_name)."%");
         	$user_list=M('follow')->field('id,nickname')->where($follow_name)->select();
         	foreach($user_list as $key=>$val){
                $uid.=$val['id'].',';
         	}
         	$map['user_id']=array('in',trim($uid,','));
         	$this->assign('user_name',$user_name,'','trim');
		 }
		  //商品名称搜索
         if($_REQUEST['futures_name']){
		 	 $futures_name=$_REQUEST['futures_name'];
		 	$futures_name_where['name']=array('like',"%".trim($futures_name)."%");
         	$futures_list=M('futures')->field('id,name')->where($futures_name_where)->select();
         	foreach($futures_list as $key=>$val){
                $fuid.=$val['id'].',';
         	}
         	$map['futures_id']=array('in',trim($fuid,','));
         	$this->assign('user_name',$futures_name,'','trim');
		 }
         
        $list = $this->lists($model,$map,'id desc');
        foreach($list as $k=>$v){
        	$where_futures['id']=array('eq',$v['futures_id']);
			$futures_name=M('futures')->where($where_futures)->getField('name');//商品名称
			$where_follow['id']=array('eq',$v['user_id']);
			$user_name=M('follow')->where($where_follow)->getField('nickname');//商品名称
			$list[$k]['futures_name']=$futures_name;
			$list[$k]['user_name']=$user_name;
		}
        $wherestr['maker_id']=array('eq',0);
        $userdata=M('follow')->where($wherestr)->select();
        if($userdata){
            $arr=array();
            foreach($userdata as $val){
                $arr[]=$val['id'];
            }
            $map['user_id']=array('not in',$arr);
        }
        if($_REQUEST['user_name']){
            $user_name=$_REQUEST['user_name'];
            $follow_name['nickname']=array('like',"%".trim($user_name)."%");
            $user_list=M('follow')->field('id,nickname')->where($follow_name)->select();
            foreach($user_list as $key=>$val){
                $uid.=$val['id'].',';
            }
            $map['user_id']=array('in',trim($uid,','));
            $this->assign('user_name',$user_name,'','trim');
        }
        $showdata=$model->field('total_price,profit')->where($map)->select();
        $topmoney="";
        $ying="";
        $kui="";
        foreach($showdata as $vv){
            $topmoney+=$vv['total_price'];
            if($vv['profit']>0){
                $ying+=$vv['profit']-$vv['total_price'];
            }else{
                $kui+=$vv['total_price'];
            }
        }
        $this->assign('topmoney',round($topmoney,2));
        $this->assign('ying',round($ying,2));
        $this->assign('kui',round($kui,2));
        $this->assign('list',$list);
		$this->display();
	}
	/**
	* 删除期货合约数据
	*/
	public function delete(){
		$ids    =   I('request.ids');
        $m=M('futures_order');
        if(is_array($ids)){
            $where = 'id in('.implode(',',$ids).')';
        }else{
            $where=array('id'=>$ids);
        }
        if($m->where($where)->delete()){
            $this->success("删除成功");
        }else{
            $this->error("删除失败");
        }
	}
	/**
     * 导出
     */

    public function download(){
    	$map=array();
		if($_REQUEST['direction']){//涨跌搜索
            if($_REQUEST['direction']==1){
                $map['direction']=array('eq',1);
                $direction=1;
            }elseif($_REQUEST['direction']==2){
                $map['direction']=array('eq',0);
                $direction=2;
            }else{
                $direction=-1;
            }
        }
        else{
            $direction=-1;
        }
        $this->assign('direction',$direction);
        if($_REQUEST['status']){//状态搜索
            if($_REQUEST['status']==1){
                $map['status']=array('eq',1);
                $status=1;
            }elseif($_REQUEST['status']==2){
                $map['status']=array('eq',2);
                $status=2;
            }elseif($_REQUEST['status']==-1){
                $map['status']=array('eq',0);
                $status=-1;
            }else{
				 $status=-2;
			}
        }
        else{
             $status=-2;
        }
        $this->assign('status',$status);
        if ($_REQUEST['begin_time']) {//交易时间
            $time=strtotime($_REQUEST['begin_time']);
            $start_time=mktime(00,00,00,date('m',$time),date('d',$time),date('Y',$time));
            $end_time=mktime(23,59,59,date('m',$time),date('d',$time),date('Y',$time));
            $map['begin_time'] = array('between',"$start_time,$end_time");
        }
         $this->assign('begin_time',$_REQUEST['begin_time']);
         
         //用户昵称搜索
         if($_REQUEST['user_name']){
		 	 $user_name=$_REQUEST['user_name'];
		 	$follow_name['nickname']=array('like',"%".trim($user_name)."%");
         	$user_list=M('follow')->field('id,nickname')->where($follow_name)->select();
         	foreach($user_list as $key=>$val){
                $uid.=$val['id'].',';
         	}
         	$map['user_id']=array('in',trim($uid,','));
         	$this->assign('user_name',$user_name,'','trim');
		 }
		  //商品名称搜索
         if($_REQUEST['futures_name']){
		 	 $futures_name=$_REQUEST['futures_name'];
		 	$futures_name_where['name']=array('like',"%".trim($futures_name)."%");
         	$futures_list=M('futures')->field('id,name')->where($futures_name_where)->select();
         	foreach($futures_list as $key=>$val){
                $fuid.=$val['id'].',';
         	}
         	$map['futures_id']=array('in',trim($fuid,','));
         	$this->assign('user_name',$futures_name,'','trim');
		 }
		$list=M('futures_order')->where($map)->order('create_time desc')->select();
		foreach($list as $k=>$v){
        	$where_futures['id']=array('eq',$v['futures_id']);//商品
			$futures_list=M('futures')->where($where_futures)->find();//商品名称		
			$list[$k]['futures_name']=$futures_list['name'];
			$list[$k]['code']=$futures_list['code'];
			$list[$k]['stop_loss']=$futures_list['stop_loss'];
			
			$where_follow['id']=array('eq',$v['user_id']);//用户名称
			$user_name=M('follow')->where($where_follow)->getField('nickname');//用户名称
			$list[$k]['user_name']=$user_name;
		}
        $twotime=date("Y-m-d H:i:s");
        $filename=$twotime.'期货合约信息.csv';
        header("Content-type:text/csv");
        header("Content-Disposition:attachment;filename=".$filename);
        header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
        header('Expires:0');
        header('Pragma:public');
        $data .= "ID,";
        $data .= "订单号,";
        $data .= "用户名,";
        $data .= "商品名称,";
        $data .= "商品代码,";
        $data .= "单价(元),";
        $data .= "总价(元),";
        $data .= "手数,";
        $data .= "权利金,";
        $data .= "止损位,";
        $data .= "开盘价,";
        $data .= "收盘价,";
        $data .= "方向,";
        $data .= "盈利,";
        $data .= "状态,";
        $data .= "开始时间,";
        $data .= "结束时间,";
        $data .= "平仓时间,";
        $data .= "创建时间,";
        $data .= "开盘价,\n";
        $i=1;
        if(!empty($list)){
            foreach($list as $k=>$v){
            	$data .= $v['id'].",";
            	$data .= $v['transaction_id']."\t,";
            	$data .= $v['user_name'].",";
            	$data .= $v['futures_name'].",";
            	$data .= $v['code'].",";
            	$data .= $v['unit_price'].",";
            	$data .= $v['total_price'].",";
            	$data .= $v['number'].",";
            	$data .= $v['premium'].",";
            	$data .= $v['stop_loss'].",";
            	$data .= $v['open_price'].",";
            	$data .= $v['close_price'].",";
            	switch ($v['direction']) {
                    case 1:
                        $direction  = '涨';
                        break;
                    case 0:
                        $direction  = '跌';
                        break;
                    default:
                        $direction  = '';
                        break;

                }
            	$data .= $direction.",";
            	$data .= $v['profit'].",";
            	switch ($v['status']) {
                    case 0:
                        $status  = '等待中';
                        break;
                    case 1:
                        $status  = '进行中';
                        break;
                    case 2:
                        $status  = '平仓成功';
                        break;
                    default:
                        $status  = '';
                        break;
                }
            	$data .= $status.",";
            	$data .= date('Y-m-d H:i:s',$v['begin_time']).",";
            	$data .= date('Y-m-d H:i:s',$v['edn_time']).",";
            	$data .= date('Y-m-d H:i:s',$v['close_time']).",";               
                $data .=date('Y-m-d H:i:s',$v['create_time'])."\t,\n";
            }
            echo  @mb_convert_encoding($data, 'GB2312','utf-8');
            exit;
        }
    }
	/**
	* 编辑期货合约
	*/
	public function edit(){
		if(IS_POST){
			$edit_where['id']=I('post.id');
			$unit_price=I('post.unit_price');
			$total_price=I('post.total_price');
			$number=I('post.number');
			$profit=I('post.profit');
			if(empty($unit_price)){
				$this->error('单价不能为空');
			}
			if(empty($total_price)){
				$this->error('单价不能为空');
			}
			$data['unit_price']=I('post.unit_price');
			if(M('futures_order')->where($edit_where)->save($data)===false){
				$this->error('编辑失败');
			}else{
				$this->success('编辑成功',U('Futures/index'));
			}
		}
		$id=I('get.id');
		$where['id']=array('eq',$id);
		$list=M('futures_order')->where($where)->find();
		$where_futures['id']=array('eq',$list['futures_id']);
		$list['futures_name']=M('futures')->where($where_futures)->getField('name');
		$where_follow['id']=array('eq',$list['user_id']);
		$list['user_name']=M('follow')->where($where_follow)->getField('nickname');
		$this->assign('list',$list);
		$this->display();
	}
}
?>